from datetime import datetime
from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_statistics.service.plant_service_service import PlantServiceService
from module_statistics.entity.vo.plant_service_vo import DeletePlantServiceModel, PlantServiceModel, \
    PlantServicePageQueryModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


plantServiceController = APIRouter(prefix='/statistics/plant_service', dependencies=[Depends(LoginService.get_current_user)])


@plantServiceController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('statistics:plantService:list'))]
)
async def get_statistics_plant_service_list(
    request: Request,
    plant_service_page_query: PlantServicePageQueryModel = Depends(PlantServicePageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    plant_service_page_query_result = await PlantServiceService.get_plant_service_list_services(
        query_db, plant_service_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=plant_service_page_query_result)


@plantServiceController.get(
    '/planterSummary', response_model=PageResponseModel, dependencies=[Depends(LoginService.get_current_user)])
async def get_planter_service_summary(
        request: Request,
        plant_service_page_query: PlantServicePageQueryModel = Depends(PlantServicePageQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
):
    # 根据查询条件
    planter_service_summary_result = await PlantServiceService.get_planter_service_summary_service(
        query_db, plant_service_page_query)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=planter_service_summary_result)

@plantServiceController.get(
    '/serviceTypeSummary', response_model=PageResponseModel, dependencies=[Depends(LoginService.get_current_user)])
async def get_service_type_summary(
        request: Request,
        service_type_query: PlantServicePageQueryModel = Depends(PlantServicePageQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
):
    # 根据查询条件
    service_type_summary_result = await PlantServiceService.get_service_type_summary_service(
        query_db, service_type_query)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=service_type_summary_result)

@plantServiceController.get(
    '/serviceMonthlySummary', response_model=PageResponseModel, dependencies=[Depends(LoginService.get_current_user)])
async def get_service_monthly_summary(
        request: Request,
        service_type_query: PlantServicePageQueryModel = Depends(PlantServicePageQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
):
    # 根据查询条件
    service_monthly_summary_result = await PlantServiceService.get_service_monthly_summary_service(
        query_db, service_type_query)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=service_monthly_summary_result)


@plantServiceController.post('', dependencies=[Depends(CheckUserInterfaceAuth('statistics:plantService:add'))])
@ValidateFields(validate_model='add_plant_service')
@Log(title='种植主体服务统计', business_type=BusinessType.INSERT)
async def add_statistics_plant_service(
    request: Request,
    add_plant_service: PlantServiceModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_plant_service_result = await PlantServiceService.add_plant_service_services(query_db, add_plant_service)
    logger.info(add_plant_service_result.message)

    return ResponseUtil.success(msg=add_plant_service_result.message)


@plantServiceController.put('', dependencies=[Depends(CheckUserInterfaceAuth('statistics:plantService:edit'))])
@ValidateFields(validate_model='edit_plant_service')
@Log(title='种植主体服务统计', business_type=BusinessType.UPDATE)
async def edit_statistics_plant_service(
    request: Request,
    edit_plant_service: PlantServiceModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_plant_service.update_by = current_user.user.user_name
    edit_plant_service.update_time = datetime.now()
    edit_plant_service_result = await PlantServiceService.edit_plant_service_services(query_db, edit_plant_service)
    logger.info(edit_plant_service_result.message)

    return ResponseUtil.success(msg=edit_plant_service_result.message)


@plantServiceController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('statistics:plantService:remove'))])
@Log(title='种植主体服务统计', business_type=BusinessType.DELETE)
async def delete_statistics_plant_service(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_plant_service = DeletePlantServiceModel(ids=ids)
    delete_plant_service_result = await PlantServiceService.delete_plant_service_services(query_db, delete_plant_service)
    logger.info(delete_plant_service_result.message)

    return ResponseUtil.success(msg=delete_plant_service_result.message)


@plantServiceController.get(
    '/{id}', response_model=PlantServiceModel, dependencies=[Depends(CheckUserInterfaceAuth('statistics:plantService:query'))]
)
async def query_detail_statistics_plant_service(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    plant_service_detail_result = await PlantServiceService.plant_service_detail_services(query_db, id)
    logger.info(f'获取id为{id}的信息成功')

    return ResponseUtil.success(data=plant_service_detail_result)


@plantServiceController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('statistics:plantService:export'))])
@Log(title='种植主体服务统计', business_type=BusinessType.EXPORT)
async def export_statistics_plant_service_list(
    request: Request,
    plant_service_page_query: PlantServicePageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    plant_service_query_result = await PlantServiceService.get_plant_service_list_services(query_db, plant_service_page_query, is_page=False)
    plant_service_export_result = await PlantServiceService.export_plant_service_list_services(plant_service_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(plant_service_export_result))
